package com.book.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.book.business.entity.BookIndent;
import com.book.business.mapper.BookIndentMapper;
import com.book.business.service.BookIndentService;
import com.book.common.entity.FebsConstant;
import com.book.common.entity.QueryRequest;
import com.book.common.utils.SortUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * @Author: 盛佳帅
 * @Description: 20173953
 */

@Service
public class BookIndentServiceImpl extends ServiceImpl<BookIndentMapper, BookIndent> implements BookIndentService {

    @Autowired
    BookIndentMapper bookIndentMapper;

    @Override
    public List<BookIndent> findBookIndent(BookIndent bookIndent) {
        QueryWrapper<BookIndent> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(bookIndent.getYear())) {
            queryWrapper.eq("year", bookIndent.getYear());
        }
        if (StringUtils.isNotEmpty(bookIndent.getMonth())) {
            queryWrapper.eq("month", bookIndent.getMonth());
        }
        if (StringUtils.isNotEmpty(bookIndent.getBackstatus())) {
            queryWrapper.eq("backstatus", bookIndent.getBackstatus());
        }
        if (StringUtils.isNotEmpty(bookIndent.getBookname())) {
            queryWrapper.eq("bookname", bookIndent.getBookname());
        }
        //做统计用
        if (StringUtils.isNotEmpty(bookIndent.getFlag())) {
            queryWrapper.eq("flag", bookIndent.getFlag());
        }
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<BookIndent> pageBookIndent(QueryRequest request, BookIndent bookIndent) {
        QueryWrapper<BookIndent> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(bookIndent.getBookname())) {
            queryWrapper.like("bookname", bookIndent.getBookname());
        }
        if (StringUtils.isNotEmpty(bookIndent.getType())) {
            queryWrapper.eq("type", bookIndent.getType());
        }
        if (StringUtils.isNotEmpty(bookIndent.getUsername())) {
            queryWrapper.eq("username", bookIndent.getUsername());
        }
        if (StringUtils.isNotEmpty(bookIndent.getOmit())) {
            queryWrapper.eq("omit", bookIndent.getOmit());
        }
        Page<BookIndent> bookIndentIPage = new Page<>(request.getPageNum(), request.getPageSize());
        return this.page(bookIndentIPage, queryWrapper);
    }

    @Override
    public void addBookIndent(BookIndent bookIndent) {
        this.save(bookIndent);
    }

    @Override
    public void updateBookIndent(BookIndent bookIndent) {
        this.saveOrUpdate(bookIndent);
    }

    @Override
    public List<BookIndent> deleteBookIndent(String[] bookIndent) {
        List<String> list = Arrays.asList(bookIndent);
        return (List<BookIndent>) listByIds(list);
    }

    @Override
    public List<BookIndent> bookIndentSort(String pubid) {
        return this.bookIndentMapper.bookIndentSort(pubid);
    }
}